
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[ScoreOdds_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[ScoreOdds_Insert]

IF OBJECT_ID(N'[dbo].[ScoreOdds_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[ScoreOdds_Update]

IF OBJECT_ID(N'[dbo].[ScoreOdds_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[ScoreOdds_Delete]

IF OBJECT_ID(N'[dbo].[ScoreOdds_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[ScoreOdds_Select]

--endregion

GO


--region [dbo].[ScoreOdds_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[ScoreOdds_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdScoreOdd int = null,
	@IdMatch int = null,
	@IdScore int = null,
	@Win float = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[ScoreOdds]
					WHERE
					(
						(@IdScoreOdd is null OR [ScoreOdds].[IdScoreOdd] = @IdScoreOdd)
						AND (@IdMatch is null OR [ScoreOdds].[IdMatch] = @IdMatch)
						AND (@IdScore is null OR [ScoreOdds].[IdScore] = @IdScore)
						AND (@Win is null OR [ScoreOdds].[Win] = @Win)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[ScoreOdds].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdScoreOdd' and @SortType = 1 THEN [ScoreOdds].[IdScoreOdd] END ASC,
						CASE WHEN @SortBy = 'IdScoreOdd' and @SortType = 0 THEN [ScoreOdds].[IdScoreOdd] END DESC,
						CASE WHEN @SortBy = 'IdMatch' and @SortType = 1 THEN [ScoreOdds].[IdMatch] END ASC,
						CASE WHEN @SortBy = 'IdMatch' and @SortType = 0 THEN [ScoreOdds].[IdMatch] END DESC,
						CASE WHEN @SortBy = 'IdScore' and @SortType = 1 THEN [ScoreOdds].[IdScore] END ASC,
						CASE WHEN @SortBy = 'IdScore' and @SortType = 0 THEN [ScoreOdds].[IdScore] END DESC,
						CASE WHEN @SortBy = 'Win' and @SortType = 1 THEN [ScoreOdds].[Win] END ASC,
						CASE WHEN @SortBy = 'Win' and @SortType = 0 THEN [ScoreOdds].[Win] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[ScoreOdds]
			Where 
			(
				(@IdScoreOdd is null OR [ScoreOdds].[IdScoreOdd] = @IdScoreOdd)
				AND (@IdMatch is null OR [ScoreOdds].[IdMatch] = @IdMatch)
				AND (@IdScore is null OR [ScoreOdds].[IdScore] = @IdScore)
				AND (@Win is null OR [ScoreOdds].[Win] = @Win)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[ScoreOdds_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[ScoreOdds_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[ScoreOdds_Insert]
	@IdScoreOdd int OUTPUT,
	@IdMatch int,
	@IdScore int,
	@Win float

AS


INSERT INTO [dbo].[ScoreOdds] 
(
	[IdMatch],
	[IdScore],
	[Win]
)
VALUES 
(
	@IdMatch,
	@IdScore,
	@Win
)

SET @IdScoreOdd = SCOPE_IDENTITY()

--end [dbo].[ScoreOdds_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[ScoreOdds_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[ScoreOdds_Update]
	@IdScoreOdd int,
	@IdMatch int,
	@IdScore int,
	@Win float
AS


UPDATE [dbo].[ScoreOdds] SET
	[IdMatch] = @IdMatch,
	[IdScore] = @IdScore,
	[Win] = @Win
WHERE
	[IdScoreOdd] = @IdScoreOdd

--end [dbo].[ScoreOdds_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[ScoreOdds_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[ScoreOdds_Delete]
	@IdScoreOdd int
AS


DELETE FROM [dbo].[ScoreOdds]
WHERE
(
	[IdScoreOdd] = @IdScoreOdd
)

--end [dbo].[ScoreOdds_Delete]
--endregion

GO
--=========================================================================================--

